<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../css/jquery-ui.css">
    <style>
        body {
            background-color: #ffffff;
        }
        .layui-form-item .layui-input-block {
            width: 400px;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input id="id" name="id" hidden>
    <div class="layui-form-item">
        <label class="layui-form-label required" for="address">影院详细地址</label>
        <div class="layui-input-block">
            <input type="text" id="cid" name="cid" hidden>
            <input type="text" id="address" name="address" lay-verify="required" lay-reqtext="地址不能为空" placeholder="请输入影院地址" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required" for="address">影院名</label>
        <div class="layui-input-block">
            <input type="text" id="cname" name="cname" lay-verify="required"  value="" class="layui-input" disabled>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required" for="address">影厅</label>
        <div class="layui-input-block">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-primary demo1">
                    <label id="hall_name" name="hallName">请选择影厅</label>
                    <input type="text" id="hall_id" name="hid" hidden></input>
                    <i class="layui-icon layui-icon-down layui-font-12"></i>
                </button>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required" for="address">电影</label>
        <div class="layui-input-block">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-primary demo2">
                    <input type="text" id="movie_id" name="mid" hidden></input>
                    <label id="movie_name" name="movieName">请选择电影</label>
                    <i class="layui-icon layui-icon-down layui-font-12"></i>
                </button>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">放映时间</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" id="start" name="start" placeholder="放映开始时间">
        </div>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" id="end" name="end" placeholder="放映结束时间" disabled>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">修改场次</button>
        </div>
    </div>
</div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="../../lib/jquery-3.4.1/jquery-ui.min.js" charset="utf-8"></script>
<script>
    function formatDate(date) {
        var d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear(),
            hour = '' + d.getHours(),
            min = '' + d.getMinutes();

        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;
        if (hour.length < 2) hour = '0' + hour;
        if (min.length < 2) min = '0' + min;
        return [year, month, day].join('-')+" "+hour+":"+min;
    }

    function setEndTime(value){
        let movieId = $("#movie_id").val();
        $.post('${path}/movies/admin/findByMovieId',{"id":movieId},function (data){
            let time = data;
            let rawdate = new Date(value.replace(/-/g, '/'))
            let new_time = new Date(rawdate.setMinutes(rawdate.getMinutes()+parseInt(time)));
            let end = formatDate(new_time);
            $('#end').val(end);
        })
    }
    jQuery(function ($) {
        $(function () {
            $("#address").autocomplete({
                serviceUrl : 'autoComplete',//load from server
                type:'post',
                paramName:'ciname',
                select: function(event,ui) {
                    // event 是当前事件对象
                    let address = ui.item.value
                    $.post('${path}/cinames/findByAddress',{"address":address},function (data) {
                        $("#cname").val(data.ciname.name);
                        $("#cid").val(data.ciname.id);
                        layui.use(['dropdown', 'layer'], function () {
                            let dropdown = layui.dropdown
                                , $ = layui.jquery;

                            dropdown.render({
                                elem: '.demo1'
                                , data: data.halls
                                , click: function (obj) {
                                    $("#hall_name").text(obj.title)
                                    $("#hall_id").text(obj.id)
                                }
                            });

                            dropdown.render({
                                elem: '.demo2'
                                , data: data.movies
                                , click: function (obj) {
                                    $("#movie_name").text(obj.title)
                                    $("#movie_id").text(obj.id)
                                    console.log(obj.id)
                                    let start = $("#start").val()
                                    if(start!=null&&start.trim().length>0){
                                        setEndTime($("#start").val())
                                    }
                                }
                            });
                        })
                    })
                    // 该对象具有label和value属性，以及其它自定义(如果有的话)的属性
                },
                source: function(request,response) {
                    $.ajax({
                        url: '${path}/cinames/findAllAddress',
                        dataType: "json",
                        data: request,
                        success: function(data) {
                            response(data);
                        },
                    });
                },

            });
        });

    });
    // 设置最小可选的日期
    function minDate(){
        let now = new Date();
        return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate();
    }


    layui.use('laydate', function(){
        let laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
            ,min: minDate()
            ,type: 'datetime'
            ,format: 'yyyy-MM-dd HH:mm'
            ,done:(value) => {
                let movieId = $("#movie_id").val();
                setEndTime(value)

                //执行指定方法
            }
        });
    });

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.confirm('确认是否修改？', {
                title: '修改',
                icon:'3'
            }, function (index) {
                let send_data = JSON.stringify(data.field);

                console.log(data.field)
                $.ajax({
                    url: '${path}/session/editSession',
                    type: "post",
                    data:  JSON.stringify(data.field),
                    contentType: 'application/json;charset=utf-8',
                    dataType:'json',
                    success: function(result){
                        if(result.code=="200"){
                            layer.msg('修改成功！', {icon:1, time: 2000},function (){
                                parent.location.reload();
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            });

                        }else {
                            layer.msg('修改失败！', {icon:2});
                        }
                    }
                });

            });

            return false;
        });
    });

    function child(data){
        layui.use(['form'], function () {
            var form = layui.form,
                $ = layui.$;
            console.log(data)
            // //获取子页面的元素，进行数据渲染
            $("#id").val(data.id);
            $("#address").val(data.address);
            $('#cname').val(data.cname);
            $("#cid").val(data.cid);
            $('#hall_id').val(data.hid);
            $('#hall_name').text(data.hallName);
            $('#start').val(data.start);
            $('#end').val(data.end);
            $("#id").val(data.id)
            $('#movie_id').val(data.mid);
            $("#movie_name").text(data.movieName)

            $.post('${path}/cinames/findByAddress',{"address":data.address},function (data) {
                layui.use(['dropdown', 'layer'], function () {
                    let dropdown = layui.dropdown
                        , $ = layui.jquery;
                    dropdown.render({
                        elem: '.demo1'
                        , data: data.halls
                        , click: function (obj) {
                            $("#hall_name").text(obj.title)
                            $("#hall_id").val(obj.id)
                        }
                    });

                    dropdown.render({
                        elem: '.demo2'
                        , data: data.movies
                        , click: function (obj) {
                            $("#movie_name").text(obj.title)
                            $("#movie_id").val(obj.id)
                            let start = $("#start").val()
                            if(start!=null&&start.trim().length>0){
                                setEndTime($("#start").val())
                            }
                        }
                    });
                })
            form.render();          //表单渲染
        });})

    }
</script>
</body>
</html>